import type { MediaFile, Category, Statistics, PaginationResponse, PaginationParams } from '@/types'

// 模拟媒体文件数据
const mockMediaFiles: MediaFile[] = [
  {
    id: '1',
    name: '精彩电影预告片.mp4',
    type: 'video',
    format: 'mp4',
    size: 1024 * 1024 * 150, // 150MB
    duration: 180,
    thumbnail: 'https://picsum.photos/300/200?random=1',
    url: 'https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_1mb.mp4',
    uploadTime: '2024-01-15 10:30:00',
    category: '电影',
    tags: ['预告片', '动作', '科幻'],
    description: '一部精彩的科幻动作电影预告片',
    views: 1250,
    likes: 89,
    status: 'ready'
  },
  {
    id: '2',
    name: '音乐MV - 流行歌曲.mp4',
    type: 'video',
    format: 'mp4',
    size: 1024 * 1024 * 80,
    duration: 240,
    thumbnail: 'https://picsum.photos/300/200?random=2',
    url: 'https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_2mb.mp4',
    uploadTime: '2024-01-14 15:20:00',
    category: '音乐',
    tags: ['MV', '流行', '音乐'],
    description: '最新流行歌曲音乐视频',
    views: 2100,
    likes: 156,
    status: 'ready'
  },
  {
    id: '3',
    name: '纯音乐 - 钢琴曲.mp3',
    type: 'audio',
    format: 'mp3',
    size: 1024 * 1024 * 8,
    duration: 300,
    url: 'https://www.soundjay.com/misc/sounds/bell-ringing-05.wav',
    uploadTime: '2024-01-13 09:15:00',
    category: '音乐',
    tags: ['钢琴', '纯音乐', '古典'],
    description: '优美的钢琴独奏曲',
    views: 890,
    likes: 67,
    status: 'ready'
  },
  {
    id: '4',
    name: '教学视频 - Vue3入门.mp4',
    type: 'video',
    format: 'mp4',
    size: 1024 * 1024 * 200,
    duration: 1800,
    thumbnail: 'https://picsum.photos/300/200?random=4',
    url: 'https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_5mb.mp4',
    uploadTime: '2024-01-12 14:45:00',
    category: '教育',
    tags: ['编程', 'Vue3', '前端'],
    description: 'Vue3框架入门教学视频',
    views: 3200,
    likes: 245,
    status: 'ready'
  },
  {
    id: '5',
    name: '新闻报道 - 科技资讯.mp4',
    type: 'video',
    format: 'mp4',
    size: 1024 * 1024 * 120,
    duration: 600,
    thumbnail: 'https://picsum.photos/300/200?random=5',
    url: 'https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_1mb.mp4',
    uploadTime: '2024-01-11 18:30:00',
    category: '新闻',
    tags: ['科技', '新闻', '资讯'],
    description: '最新科技行业新闻报道',
    views: 1800,
    likes: 92,
    status: 'ready'
  }
]

// 模拟API延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

// API函数
export const mediaApi = {
  // 获取媒体文件列表
  async getMediaFiles(params: PaginationParams): Promise<PaginationResponse<MediaFile>> {
    await delay(500)
    
    let filteredFiles = [...mockMediaFiles]
    
    // 关键词搜索
    if (params.keyword) {
      filteredFiles = filteredFiles.filter(file => 
        file.name.toLowerCase().includes(params.keyword!.toLowerCase()) ||
        file.tags.some(tag => tag.toLowerCase().includes(params.keyword!.toLowerCase()))
      )
    }
    
    // 分类筛选
    if (params.category) {
      filteredFiles = filteredFiles.filter(file => file.category === params.category)
    }
    
    // 类型筛选
    if (params.type) {
      filteredFiles = filteredFiles.filter(file => file.type === params.type)
    }
    
    const start = (params.page - 1) * params.pageSize
    const end = start + params.pageSize
    
    return {
      list: filteredFiles.slice(start, end),
      total: filteredFiles.length,
      page: params.page,
      pageSize: params.pageSize
    }
  },

  // 获取单个媒体文件
  async getMediaFile(id: string): Promise<MediaFile | null> {
    await delay(300)
    return mockMediaFiles.find(file => file.id === id) || null
  },

  // 删除媒体文件
  async deleteMediaFile(id: string): Promise<boolean> {
    await delay(500)
    const index = mockMediaFiles.findIndex(file => file.id === id)
    if (index > -1) {
      mockMediaFiles.splice(index, 1)
      return true
    }
    return false
  },

  // 更新媒体文件
  async updateMediaFile(id: string, data: Partial<MediaFile>): Promise<MediaFile | null> {
    await delay(500)
    const index = mockMediaFiles.findIndex(file => file.id === id)
    if (index > -1) {
      mockMediaFiles[index] = { ...mockMediaFiles[index], ...data }
      return mockMediaFiles[index]
    }
    return null
  }
} 